/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.slayer.service.impl;

import java.util.Date;
import java.util.List;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.Property;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.service.ServiceContext;
import com.slayer.model.LMSBook;
import com.slayer.model.LMSBorrowing;
import com.slayer.model.impl.LMSBookImpl;
import com.slayer.service.LMSBookLocalServiceUtil;
import com.slayer.service.base.LMSBookLocalServiceBaseImpl;
import com.slayer.service.persistence.LMSBookPersistence;
import com.util.LMSUtil;

/**
 * The implementation of the l m s book local service.
 * 
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link com.slayer.service.LMSBookLocalService} interface.
 * 
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 * 
 * @author Ashish Joshi
 * @see com.slayer.service.base.LMSBookLocalServiceBaseImpl
 * @see com.slayer.service.LMSBookLocalServiceUtil
 */
public class LMSBookLocalServiceImpl extends LMSBookLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 * 
	 * Never reference this interface directly. Always use {@link
	 * com.slayer.service.LMSBookLocalServiceUtil} to access the l m s book
	 * local service.
	 */

	public LMSBook insertBook(String bookTitle, String author,
			ServiceContext context) {
		LMSBook lmsBook = new LMSBookImpl();
		long bookId = 0l;

		try {
			bookId = CounterLocalServiceUtil.increment();
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		lmsBook.setBookId(bookId);
		//lmsBook.setCompanyId(context.getCompanyId());
		lmsBook.setAuthor(author);
		lmsBook.setBookTitle(bookTitle);
		lmsBook.setCreateDate(new Date());
		lmsBook.setModifiedDate(new Date());
		lmsBook.setGroupId(context.getScopeGroupId());
		lmsBook.setCompanyId(context.getCompanyId());
		//lmsBook.setUserId(context.getUserId());

		try {
			LMSBookLocalServiceUtil.addLMSBook(lmsBook);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lmsBook;
	}

	/* finder search method */
	public List<LMSBook> searchBooks(String bookTitle) throws SystemException {
		// return lmsBookPersistence.findByBookTitle(bookTitle);
		System.out.println("Using dynamic query for search");
		DynamicQuery dynamicQuery = DynamicQueryFactoryUtil
				.forClass(LMSBook.class);
		Property bookTitleProperty = PropertyFactoryUtil.forName("bookTitle");
		dynamicQuery.add(bookTitleProperty.like("%" + bookTitle + "%"));
		return dynamicQuery(dynamicQuery);
	}

	public List<LMSBorrowing> getBorrowings(long bookId) throws SystemException {
		return lmsBookPersistence.getLMSBorrowings(bookId);
	}
	
	public List<LMSBook> getLMSBooskByCompanyIdGroupId(long companyId, long groupId) throws SystemException {
		return lmsBookPersistence.findByCompanyIdGroupId(groupId, companyId);
	}

}